<template>
	<uni-popup ref="popup" @change="handleChange" v-if="params">
		<view class="popup simple-card round-corner uni-flex uni-column align-center">
			<text>关注“第三空间{{ params.nameCity || '' }}站”公众号，实时掌握收益变化！</text>
			<view class="img-qrcode t-mg b-mg">
				<u-lazy-load :image="imgUrl" img-mode="aspectFit" :height="480"></u-lazy-load>
			</view>
			<view class="uni-flex">
				<u-button @click="handleSaveQRCode" shape="circle" type="primary">保存二维码</u-button>
				<view class="l-pad"></view>
				<u-button @click="handleCopy" shape="circle" type="primary">复制公众号</u-button>
			</view>
			<!-- <text class="t-mg" @click="handleNotRemind">已关注，不再提示</text> -->
		</view>
	</uni-popup>
</template>

<script>
	export default {
		data() {
			return {
				imgUrl: ''
			};
		},
		props: {
			value: {
				type: Boolean,
				default: false
			},
			params: Object
		},
		watch: {
			params(newValue, oldValue) {
				this.imgUrl = `${this.$cloudUrl}/genWXPubQRCode?city=${newValue.codeCity}&sceneStr=1&_t=${Date.now()}`
			},
			value(newVal) {
				console.log("visible changed", newVal);
				if (newVal) {
					this.$refs.popup.open();
				} else {
					this.$refs.popup.close();
				}
			}
		},
		methods: {
			handleChange(e) {
				this.$emit("input", e.show);
			},
			async handleCopy() {
				let { wxPubConfig: { account } } = await this.$dbHelper.getOne("t3ds-region", {where: {'city.value': this.params.codeCity}})
				this.$copy(account)
			},
			handleNotRemind() {
				uni.setStorageSync("notRemind", true);
				this.$refs.popup.close();
			},
			async handleSaveQRCode() {
				uni.showLoading({
					mask: true
				})
				try {
					await new Promise((resolve, reject) => {
						uni.getSetting({
							success: (res) => {
								let t = res.authSetting["scope.writePhotosAlbum"];
								if (t !== undefined && !t) {
									// 请求过权限，但是用户关闭了
									uni.showModal({
										title: "请先打开相册开关",
										cancelText: "取消",
										confirmText: "去打开",
										success: (res) => {
											if (res.confirm) {
												uni.openSetting();
											}
										}
									})
									rejcet();
								} else {
									resolve();
								}
							},
							fail: (err) => {
								resolve();
							}
						})
					})
				} catch (e) {
					uni.hideLoading();
					return;
				}
				
				let imagePath;
				try {
					imagePath = await new Promise((resolve, reject) => {
						uni.getImageInfo({
							src: "../static/img/pub_qrcode.jpg",
							success: (image) => {
								resolve(image.path);
								console.log('获取图片成功', image)
							},
							fail: (err) => {
								reject(err);
								console.log('获取图片失败', err)
							}
						});
					});
				} catch (e) {
					this.$log("获取二维码路径遇到错误", e);
					uni.hideLoading();
					return;
				}
				
				uni.saveImageToPhotosAlbum({
					filePath: imagePath,
					success: (res) => {
						uni.hideLoading();
						uni.showToast({
							title: '已保存到相册',
							icon: 'success',
							duration: 2000
						})
						this.$refs.popup.close();
					},
					fail: (e) => {
						uni.showToast({
							title: '保存失败',
							icon: 'none',
							duration: 2000
						})
						uni.hideLoading();
					}
				})
			},
		}
	}
</script>

<style scoped>
	.img-qrcode {
		width: 480rpx;
		height: 480rpx;
	}
</style>
